{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "35a73037",
   "metadata": {},
   "source": [
    "# 第五周学习笔记\n",
    "## 一、列表的概念\n",
    "1.序列是 Python 中最基本的数据结构。   \n",
    "2.序列中的每个值都有对应的位置值，称之为索引，第一个索引是 0，第二个索引是 1，依此类推。   \n",
    "3.Python 有 6 个序列的内置类型，但最常见的是列表和元组。   \n",
    "4.列表都可以进行的操作包括索引，切片，加，乘，检查成员。   \n",
    "5.此外，Python 已经内置确定序列的长度以及确定最大和最小的元素的方法。   \n",
    "6.列表是最常用的 Python 数据类型，它可以作为一个方括号内的逗号分隔值出现。   \n",
    "7.列表的数据项不需要具有相同的类型。   \n",
    "8.创建一个列表，只要把逗号分隔的不同的数据项使用方括号括起来即可。   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "71896912",
   "metadata": {},
   "outputs": [],
   "source": [
    "#　 列表可包含任何数据类型的元素，单个列表中的元素无须全为同一类型。\n",
    "list1 = ['Google', 'Runoob', 1997, 2000]\n",
    "list2 = [1, 2, 3, 4, 5 ]\n",
    "list3 = [\"a\", \"b\", \"c\", \"d\"]\n",
    "list4 = ['red', 'green', 'blue', 'yellow', 'white', 'black']\n",
    "#除了使用[ ]创建列表外，Python 还提供了一个内置的函数 list()，使用它可以将其它数据类型转换为列表类型。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db0df6b2",
   "metadata": {},
   "source": [
    "## 二、列表的认识"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "85854d38",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_data_list = [[\"zhichao\",123435,19],[\"Jack\",463434,23],[\"Alex\",583495,25],[\"Marry\",438294,27]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d07ee120",
   "metadata": {},
   "source": [
    "### 1. 从左往右取值（index & slice）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "708a5400",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Alex', 583495, 25]\n",
      "[['zhichao', 123435, 19], ['Jack', 463434, 23], ['Alex', 583495, 25]]\n",
      "[['zhichao', 123435, 19], ['Jack', 463434, 23], ['Alex', 583495, 25]]\n"
     ]
    }
   ],
   "source": [
    "# 从左往右取值（index & slice）\n",
    "print(user_data_list[2])\n",
    "# 切片 slice ，右边的值取不到 例如[0:3] 索引为3的值取不到\n",
    "print(user_data_list[0:3])\n",
    "# 当从0开始取值时，0 可以忽略不写\n",
    "print(user_data_list[:3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5f84673",
   "metadata": {},
   "source": [
    "### 2.从右往左取值(index & slice)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8834bb1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Alex', 583495, 25]\n",
      "[['Jack', 463434, 23], ['Alex', 583495, 25]]\n",
      "[]\n",
      "[['Jack', 463434, 23], ['Alex', 583495, 25], ['Marry', 438294, 27]]\n"
     ]
    }
   ],
   "source": [
    "# 从右往左取值(index & slice)\n",
    "print(user_data_list[-2])\n",
    "# 切片 slice ，右边的值取不到 例如[-3:-1] 索引为-1的值取不到\n",
    "print(user_data_list[-3:-1])\n",
    "# 如果取值为空：说明slice写错了 例如[-3:0]\n",
    "# 如果要取到最后一个值，不能写0，直接省略不写\n",
    "print(user_data_list[-3:0])\n",
    "print(user_data_list[-3:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a8f4332b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['zhichao', 123435, 19], ['Jack', 463434, 23], ['Alex', 583495, 25], ['Marry', 438294, 27]]\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "[0, 2, 4]\n"
     ]
    }
   ],
   "source": [
    "# ****重要****\n",
    "print(user_data_list[:])\n",
    "\n",
    "# 3.步长，list列表slice ：[start:stop:step]\n",
    "'''\n",
    "如果没有指定开始值，则默认为0；\n",
    "如果没有指定结束指，则取列表允许的最大值；\n",
    "如果没有指定步长值，则默认步长为1.\n",
    "'''\n",
    "num = list(range(10))\n",
    "print(num)\n",
    "print(num[0:6:2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78b62b8e",
   "metadata": {},
   "source": [
    "## 三、序列的应用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63e158ae",
   "metadata": {},
   "source": [
    "### 1.序列相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2d123ca1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python编程官网:https://www.python.org/\n"
     ]
    }
   ],
   "source": [
    "str=\"https://www.python.org/\"\n",
    "print(\"Python编程\"+\"官网:\"+str)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e442fcea",
   "metadata": {},
   "source": [
    "### 2.序列相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "174995e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python编程官网Python编程官网Python编程官网\n",
      "[None, None, None, None, None]\n"
     ]
    }
   ],
   "source": [
    "str=\"Python编程官网\"\n",
    "print(str*3)\n",
    "#特殊：列表类型在进行乘法运算时，还可以实现初始化指定长度列表的功能。例如如下的代码，将创建一个长度为 5 的列表，列表中的每个元素都是 None，表示什么都没有。\n",
    "list = [None]*5\n",
    "print(list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "070623da",
   "metadata": {},
   "source": [
    "### 3.检查元素是否包含在序列中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1f5c6dc2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "str=\"python.org\"\n",
    "print('python'in str)\n",
    "#或者可以采用not in关键字\n",
    "str=\"python.org\"\n",
    "print('python' not in str)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf8b4bef",
   "metadata": {},
   "source": [
    "### 4.序列相关内置函数表\n",
    "| 函数 | 功能 |\n",
    "|  :----  |  :----  |\n",
    "|len()|计算序列的长度，即返回序列中包含多少个元素。|\n",
    "|max()|找出序列中的最大元素。注意，对序列使用 sum() 函数时，做加和操作的必须都是数字，不能是字符或字符串，否则该函数将抛出异常，因为解释器无法判定是要做连接操作（+ 运算符可以连接两个序列），还是做加和操作。|\n",
    "|min()|找出序列中的最小元素。|\n",
    "|list()|将序列转换为列表。|\n",
    "|str()|将序列转换为字符串。|\n",
    "|sum()|计算元素和。|\n",
    "|sorted()|对元素进行排序。|\n",
    "|reversed()|反向序列中的元素。|\n",
    "|enumerate()|将序列组合为一个索引序列，多用在 for 循环中。|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e28a817",
   "metadata": {},
   "source": [
    "### 5.序列相关内置函数表案例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "62d0e5ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "y\n",
      ".\n",
      "['.', 'g', 'h', 'n', 'o', 'o', 'p', 'r', 't', 'y']\n"
     ]
    }
   ],
   "source": [
    "str=\"python.org\"\n",
    "#找出最大的字符\n",
    "print(max(str))\n",
    "#找出最小的字符\n",
    "print(min(str))\n",
    "#对字符串中的元素进行排序\n",
    "print(sorted(str))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09529942",
   "metadata": {},
   "source": [
    "## 四、列表的应用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adb691ef",
   "metadata": {},
   "source": [
    "### 1.使用append()方法添加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8c1d1add",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第三个元素为 :  1997\n",
      "更新后的第三个元素为 :  2001\n",
      "更新后的列表 :  ['Google', 'Runoob', 'Taobao', 'Baidu']\n"
     ]
    }
   ],
   "source": [
    "#1.追加元素\n",
    "list = ['Google', 'Runoob', 1997, 2000]\n",
    "print (\"第三个元素为 : \", list[2])\n",
    "list[2] = 2001\n",
    "print (\"更新后的第三个元素为 : \", list[2])\n",
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "list1.append('Baidu')\n",
    "print (\"更新后的列表 : \", list1)\n",
    "# append（）方法用于在列表末尾添加新的对象，该方法无法返回值（返回值就是代表着可以被调用），但是会修改原来的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "168f64b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'C', 'Java', ('JavaScript', 'C++', 'Go')]\n",
      "['Python', 'C', 'Java', ('JavaScript', 'C++', 'Go'), ['Html', 'SQL']]\n"
     ]
    }
   ],
   "source": [
    "#2.追加元组，整个元组被当成一个元素\n",
    "l = ['Python', 'C', 'Java']\n",
    "t = ('JavaScript', 'C++', 'Go')\n",
    "l.append(t)\n",
    "print(l)\n",
    "#3.追加列表，整个列表也被当成一个元素\n",
    "l.append(['Html', 'SQL'])\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3454ca3c",
   "metadata": {},
   "source": [
    "### 2.使用extend()方法添加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "0db56ac0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'C++', 'Java', 'C']\n",
      "['Python', 'C++', 'Java', 'C', 'JavaScript', 'C#', 'Go']\n",
      "['Python', 'C++', 'Java', 'C', 'JavaScript', 'C#', 'Go', 'Ruby', 'SQL']\n"
     ]
    }
   ],
   "source": [
    "l = ['Python', 'C++', 'Java']\n",
    "#1.追加元素\n",
    "l.extend('C')\n",
    "print(l)\n",
    "#2.追加元组，元祖被拆分成多个元素\n",
    "t = ('JavaScript', 'C#', 'Go')\n",
    "l.extend(t)\n",
    "print(l)\n",
    "#3.追加列表，列表也被拆分成多个元素\n",
    "l.extend(['Ruby', 'SQL'])\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8164299",
   "metadata": {},
   "source": [
    "### 3.使用insert()方法插入元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "7e9345d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'C', 'C++', 'Java']\n",
      "['Python', 'C', ('C#', 'Go'), 'C++', 'Java']\n",
      "['Python', 'C', ('C#', 'Go'), ['Ruby', 'SQL'], 'C++', 'Java']\n",
      "['http://c.biancheng.net', 'Python', 'C', ('C#', 'Go'), ['Ruby', 'SQL'], 'C++', 'Java']\n"
     ]
    }
   ],
   "source": [
    "l = ['Python', 'C++', 'Java']\n",
    "#1.插入元素\n",
    "l.insert(1, 'C')\n",
    "print(l)\n",
    "#2.插入元组，整个元祖被当成一个元素\n",
    "t = ('C#', 'Go')\n",
    "l.insert(2, t)\n",
    "print(l)\n",
    "#3.插入列表，整个列表被当成一个元素\n",
    "l.insert(3, ['Ruby', 'SQL'])\n",
    "print(l)\n",
    "#4.插入字符串，整个字符串被当成一个元素\n",
    "l.insert(0, \"http://c.biancheng.net\")\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e1a5f9e",
   "metadata": {},
   "source": [
    "### 4.使用 del/pop/remove() 方法删除列表元素"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca8e36e3",
   "metadata": {},
   "source": [
    "#### del：根据索引值删除元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "938e5db8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'C++', 'PHP', 'Ruby', 'MATLAB']\n",
      "['Python', 'C++', 'PHP', 'MATLAB']\n",
      "['Python', 'Ruby', 'MATLAB']\n",
      "['Python', 'C#', 'Go']\n"
     ]
    }
   ],
   "source": [
    "lang = [\"Python\", \"C++\", \"Java\", \"PHP\", \"Ruby\", \"MATLAB\"]\n",
    "#使用正数索引\n",
    "del lang[2]\n",
    "print(lang)\n",
    "#使用负数索引\n",
    "del lang[-2]\n",
    "print(lang)\n",
    "#使用 del 删除一段连续的元素\n",
    "lang = [\"Python\", \"C++\", \"Java\", \"PHP\", \"Ruby\", \"MATLAB\"]\n",
    "del lang[1: 4]\n",
    "print(lang)\n",
    "lang.extend([\"SQL\", \"C#\", \"Go\"])\n",
    "del lang[-5: -2]\n",
    "print(lang)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7829f1b",
   "metadata": {},
   "source": [
    "#### pop()：根据索引值删除元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "fe29ec81",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[40, 36, 89, 36, 100, 7]\n",
      "[40, 36, 89, 36, 100]\n"
     ]
    }
   ],
   "source": [
    "nums = [40, 36, 89, 2, 36, 100, 7]\n",
    "nums.pop(3)\n",
    "print(nums)\n",
    "nums.pop()\n",
    "print(nums)\n",
    "#如果不写 index 参数，默认会删除列表中的最后一个元素，类似于数据结构中的“出栈”操作。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd01b9d3",
   "metadata": {},
   "source": [
    "#### remove()：根据元素值进行删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "87f8bb56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[40, 89, 2, 36, 100, 7]\n",
      "[40, 89, 2, 100, 7]\n"
     ]
    }
   ],
   "source": [
    "nums = [40, 36, 89, 2, 36, 100, 7]\n",
    "#第一次删除36\n",
    "nums.remove(36)\n",
    "print(nums)\n",
    "#第二次删除36\n",
    "nums.remove(36)\n",
    "print(nums)\n",
    "#remove() 方法只会删除第一个和指定值相同的元素，而且必须保证该元素是存在的，否则会引发 ValueError 错误。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cc4e36d",
   "metadata": {},
   "source": [
    "#### clear()：删除列表所有元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "b9515d95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "#clear()用来删除列表的所有元素，也即清空列表。\n",
    "list=[\"python.org\"]\n",
    "url = list\n",
    "url.clear()\n",
    "print(url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bad29b0",
   "metadata": {},
   "source": [
    "### 5.列表应用方法总结\n",
    "| 序号 | 方法 | 功能 |\n",
    "|:----:|  :----  |  :----  |\n",
    "| 1 |list.append(obj)|在列表末尾添加新的对象|\n",
    "| 2 |list.count(obj)|统计某个元素在列表中出现的次数|\n",
    "| 3 |list.extend(seq)|在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）|\n",
    "| 4 |list.index(obj)|从列表中找出某个值第一个匹配项的索引位置|\n",
    "| 5 |list.insert(index, obj)|将对象插入列表|\n",
    "| 6 |list.pop([index=-1])|移除列表中的一个元素（默认最后一个元素），并且返回该元素的值|\n",
    "| 7 |list.remove(obj)|移除列表中某个值的第一个匹配项|\n",
    "| 8 |list.reverse()|反向列表中元素|\n",
    "| 9 |list.sort( key=None, reverse=False)|对原列表进行排序|\n",
    "| 10 |list.clear()|清空列表|\n",
    "| 11 |list.copy()|复制列表|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "8de02770",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "更新后的列表 :  ['Google', 'Runoob', 'Taobao', 'Baidu']\n"
     ]
    }
   ],
   "source": [
    "#1\n",
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "list1.append('Baidu')\n",
    "print (\"更新后的列表 : \", list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "7146d644",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123 元素个数 :  2\n",
      "Runoob 元素个数 :  1\n"
     ]
    }
   ],
   "source": [
    "#2\n",
    "aList = [123, 'Google', 'Runoob', 'Taobao', 123];\n",
    "print (\"123 元素个数 : \", aList.count(123))\n",
    "print (\"Runoob 元素个数 : \", aList.count('Runoob'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "e39a4626",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "扩展后的列表： ['Google', 'Runoob', 'Taobao', 0, 1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "#3\n",
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "list=range(5)\n",
    "list2=list # 创建 0-4 的列表 list2=list(range(5)) jupyter不支持直接创建\n",
    "list1.extend(list2)  # 扩展列表\n",
    "print (\"扩展后的列表：\", list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "4105ec15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Runoob 索引值为 1\n",
      "Taobao 索引值为 2\n"
     ]
    }
   ],
   "source": [
    "#4\n",
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "print ('Runoob 索引值为', list1.index('Runoob'))\n",
    "print ('Taobao 索引值为', list1.index('Taobao'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "9a0bf585",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表插入元素后为 :  ['Google', 'Baidu', 'Runoob', 'Taobao']\n"
     ]
    }
   ],
   "source": [
    "#5\n",
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "list1.insert(1, 'Baidu')\n",
    "print ('列表插入元素后为 : ', list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "8b75ad58",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表现在为 :  ['Google', 'Runoob']\n",
      "列表现在为 :  ['Google']\n"
     ]
    }
   ],
   "source": [
    "#6\n",
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "list1.pop()\n",
    "print (\"列表现在为 : \", list1)\n",
    "list1.pop(1)\n",
    "print (\"列表现在为 : \", list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "6424a3b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表现在为 :  ['Google', 'Runoob', 'Baidu']\n",
      "列表现在为 :  ['Google', 'Runoob']\n"
     ]
    }
   ],
   "source": [
    "#7\n",
    "list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']\n",
    "list1.remove('Taobao')\n",
    "print (\"列表现在为 : \", list1)\n",
    "list1.remove('Baidu')\n",
    "print (\"列表现在为 : \", list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "96b772d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表反转后:  ['Baidu', 'Taobao', 'Runoob', 'Google']\n"
     ]
    }
   ],
   "source": [
    "#8\n",
    "list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']\n",
    "list1.reverse()\n",
    "print (\"列表反转后: \", list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "401e5dc2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List :  ['Facebook', 'Google', 'Runoob', 'Taobao']\n"
     ]
    }
   ],
   "source": [
    "#9\n",
    "aList = ['Google', 'Runoob', 'Taobao', 'Facebook']\n",
    "aList.sort()\n",
    "print ( \"List : \", aList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "35b20293",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表清空后 :  []\n"
     ]
    }
   ],
   "source": [
    "#10\n",
    "list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']\n",
    "list1.clear()\n",
    "print (\"列表清空后 : \", list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "bd4caa76",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "list2 列表:  ['Google', 'Runoob', 'Taobao', 'Baidu']\n"
     ]
    }
   ],
   "source": [
    "#11\n",
    "list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']\n",
    "list2 = list1.copy()\n",
    "print (\"list2 列表: \", list2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "2514e81c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name: caihongjia\n",
      "age: 20\n",
      "job: student\n",
      "salary: 1500\n",
      "-----------INFO caihongjia----------\n",
      "name:caihongjia\n",
      "age:20\n",
      "job:student\n",
      "salary:1500\n"
     ]
    }
   ],
   "source": [
    "# 用户输入和格式化打印\n",
    "\n",
    "name = input(\"name: \")\n",
    "age = input(\"age: \")\n",
    "job = input(\"job: \")\n",
    "salary = input(\"salary: \")\n",
    "\n",
    "# name 动态的出现在字符串中,字符串的拼接 用 \"+\" 符号来拼接\n",
    "info = '''-----------INFO '''+ name+ '''----------\n",
    "name:'''+name+'''\n",
    "age:'''+age+'''\n",
    "job:'''+job+'''\n",
    "salary:'''+salary\n",
    "\n",
    "print(info)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "cf4652a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----------INFO caihongjia----------\n",
      "name:caihongjia\n",
      "age:20\n",
      "job:student\n",
      "salary:1500\n"
     ]
    }
   ],
   "source": [
    "# 优化字符串拼接 ： 占位符 %s 动态的数据-->变量\n",
    "# 优势：1.全部都写在一个大的字符串里面了\n",
    "#      2.省略了 “+”符号\n",
    "# 缺点？ 如果有几百个需要填进去的内容？容易顺序乱掉！出现打印结果的不正确\n",
    "\n",
    "info_占位符 = '''-----------INFO %s----------\n",
    "name:%s\n",
    "age:%s\n",
    "job:%s\n",
    "salary:%s''' %(name,name,age,job,salary)\n",
    "\n",
    "print(info_占位符)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "3b9f8e65",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----------INFO caihongjia----------\n",
      "name:caihongjia\n",
      "age:20\n",
      "job:student\n",
      "salary:1500\n"
     ]
    }
   ],
   "source": [
    "# 优化： .format() 格式化 字符串\n",
    "\n",
    "info_format = '''-----------INFO {姓名}----------\n",
    "name:{姓名}\n",
    "age:{年龄}\n",
    "job:{工作}\n",
    "salary:{薪水}'''.format(年龄=age,姓名=name,工作=job,薪水=salary)\n",
    "\n",
    "print(info_format)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "6e901242",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.nfu.edu.cn/ztb/index1.htm\n",
      "https://www.nfu.edu.cn/ztb/index2.htm\n",
      "https://www.nfu.edu.cn/ztb/index3.htm\n",
      "https://www.nfu.edu.cn/ztb/index4.htm\n",
      "https://www.nfu.edu.cn/ztb/index5.htm\n",
      "https://www.nfu.edu.cn/ztb/index6.htm\n",
      "https://www.nfu.edu.cn/ztb/index7.htm\n",
      "https://www.nfu.edu.cn/ztb/index8.htm\n",
      "https://www.nfu.edu.cn/ztb/index9.htm\n",
      "https://www.nfu.edu.cn/ztb/index10.htm\n",
      "https://www.nfu.edu.cn/ztb/index11.htm\n",
      "https://www.nfu.edu.cn/ztb/index12.htm\n",
      "https://www.nfu.edu.cn/ztb/index13.htm\n",
      "https://www.nfu.edu.cn/ztb/index14.htm\n",
      "https://www.nfu.edu.cn/ztb/index15.htm\n",
      "https://www.nfu.edu.cn/ztb/index16.htm\n",
      "https://www.nfu.edu.cn/ztb/index17.htm\n",
      "https://www.nfu.edu.cn/ztb/index18.htm\n",
      "https://www.nfu.edu.cn/ztb/index19.htm\n",
      "https://www.nfu.edu.cn/ztb/index20.htm\n",
      "https://www.nfu.edu.cn/ztb/index21.htm\n",
      "https://www.nfu.edu.cn/ztb/index22.htm\n",
      "https://www.nfu.edu.cn/ztb/index23.htm\n",
      "https://www.nfu.edu.cn/ztb/index24.htm\n",
      "https://www.nfu.edu.cn/ztb/index25.htm\n",
      "https://www.nfu.edu.cn/ztb/index26.htm\n"
     ]
    }
   ],
   "source": [
    "# .format()案例\n",
    "\n",
    "url = \"https://www.nfu.edu.cn/\"  #官网的域名\n",
    "# 目标获取高教动态的所有url链接\n",
    "高教动态 = \"gjdt\"\n",
    "url_页面 = \".htm\"\n",
    "url_页面细节不变 = \"/index\"\n",
    "\n",
    "# for i in range(1,27):\n",
    "#     完整的高教动态所有页面url = url+高教动态+ url_页面细节不变 + str(i)+url_页面\n",
    "#     print(完整的高教动态所有页面url)\n",
    "\n",
    "for i in range(1,27):\n",
    "    完整的高教动态所有页面url = \"https://www.nfu.edu.cn/{新闻}/index{页码}.htm\".format(页码=i,新闻=\"ztb\")\n",
    "    print(完整的高教动态所有页面url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61e06bb8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
